using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qABPOS = from m in mtemp.ABPOS select m;
       DBMapper.MapABPOS(qABPOS.ToList(), true);
       */
        public List<ABPOS> MapABPOS(XElement item2, bool mergedb)
        {
            List<ABPOS> result = new List<ABPOS>();
            var item4 = from item3 in item2.Elements()
                        select new ABPOS
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            ABPMANDANT = (string)item3.Element("ABPMANDANT"),
                            ABPAUFKRS = (string)item3.Element("ABPAUFKRS"),
                            ABPAUFLFD = (string)item3.Element("ABPAUFLFD"),
                            ABPAUFPOS = (string)item3.Element("ABPAUFPOS"),
                            ABPWAKZ = (string)item3.Element("ABPWAKZ"),
                            ABPART = (string)item3.Element("ABPART"),
                            ABPVAR = (string)item3.Element("ABPVAR"),
                            ABPFARBE = (string)item3.Element("ABPFARBE"),
                            ABPSOLLMENGE1 = (string)item3.Element("ABPSOLLMENGE1"),
                            ABPSOLLMENGE2 = (string)item3.Element("ABPSOLLMENGE2"),
                            ABPSOLLMENGE3 = (string)item3.Element("ABPSOLLMENGE3"),
                            ABPSOLLMENGE4 = (string)item3.Element("ABPSOLLMENGE4"),
                            ABPSOLLMENGE5 = (string)item3.Element("ABPSOLLMENGE5"),
                            ABPSOLLMENGE6 = (string)item3.Element("ABPSOLLMENGE6"),
                            ABPSOLLMENGE7 = (string)item3.Element("ABPSOLLMENGE7"),
                            ABPSOLLMENGE8 = (string)item3.Element("ABPSOLLMENGE8"),
                            ABPLIEFDAT = (string)item3.Element("ABPLIEFDAT"),
                            ABPSTARTDAT = (string)item3.Element("ABPSTARTDAT"),
                            ABPNEUDAT = (string)item3.Element("ABPNEUDAT"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapABPOS(result, mergedb);
          return result;
        }
        ///DBMapper.MapABPOS(mtemp.ABPOS.ToList<ABPOS>(), true);        
       public void MapABPOS(List<ABPOS> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from ABPOS row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.ABPOS.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.ABPOS.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.ABPOS.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "ABPOS")]
    public class ABPOS : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _ABPMANDANT="0";
        [Column]
        public string ABPMANDANT
        {
            get
            {
                return _ABPMANDANT;
            }
            set
            {
                if (value != _ABPMANDANT)
                {
                     _ABPMANDANT = value;
                    NotifyPropertyChanged("ABPMANDANT");
                }
            }
        }
        private string  _ABPAUFKRS="0";
        [Column]
        public string ABPAUFKRS
        {
            get
            {
                return _ABPAUFKRS;
            }
            set
            {
                if (value != _ABPAUFKRS)
                {
                     _ABPAUFKRS = value;
                    NotifyPropertyChanged("ABPAUFKRS");
                }
            }
        }
        private string  _ABPAUFLFD="0";
        [Column]
        public string ABPAUFLFD
        {
            get
            {
                return _ABPAUFLFD;
            }
            set
            {
                if (value != _ABPAUFLFD)
                {
                     _ABPAUFLFD = value;
                    NotifyPropertyChanged("ABPAUFLFD");
                }
            }
        }
        private string  _ABPAUFPOS="0";
        [Column]
        public string ABPAUFPOS
        {
            get
            {
                return _ABPAUFPOS;
            }
            set
            {
                if (value != _ABPAUFPOS)
                {
                     _ABPAUFPOS = value;
                    NotifyPropertyChanged("ABPAUFPOS");
                }
            }
        }
        private string  _ABPWAKZ="0";
        [Column]
        public string ABPWAKZ
        {
            get
            {
                return _ABPWAKZ;
            }
            set
            {
                if (value != _ABPWAKZ)
                {
                     _ABPWAKZ = value;
                    NotifyPropertyChanged("ABPWAKZ");
                }
            }
        }
        private string  _ABPART="0";
        [Column]
        public string ABPART
        {
            get
            {
                return _ABPART;
            }
            set
            {
                if (value != _ABPART)
                {
                     _ABPART = value;
                    NotifyPropertyChanged("ABPART");
                }
            }
        }
        private string  _ABPVAR="0";
        [Column]
        public string ABPVAR
        {
            get
            {
                return _ABPVAR;
            }
            set
            {
                if (value != _ABPVAR)
                {
                     _ABPVAR = value;
                    NotifyPropertyChanged("ABPVAR");
                }
            }
        }
        private string  _ABPFARBE="0";
        [Column]
        public string ABPFARBE
        {
            get
            {
                return _ABPFARBE;
            }
            set
            {
                if (value != _ABPFARBE)
                {
                     _ABPFARBE = value;
                    NotifyPropertyChanged("ABPFARBE");
                }
            }
        }
        private string  _ABPSOLLMENGE1="0";
        [Column]
        public string ABPSOLLMENGE1
        {
            get
            {
                return _ABPSOLLMENGE1;
            }
            set
            {
                if (value != _ABPSOLLMENGE1)
                {
                     _ABPSOLLMENGE1 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE1");
                }
            }
        }
        private string  _ABPSOLLMENGE2="0";
        [Column]
        public string ABPSOLLMENGE2
        {
            get
            {
                return _ABPSOLLMENGE2;
            }
            set
            {
                if (value != _ABPSOLLMENGE2)
                {
                     _ABPSOLLMENGE2 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE2");
                }
            }
        }
        private string  _ABPSOLLMENGE3="0";
        [Column]
        public string ABPSOLLMENGE3
        {
            get
            {
                return _ABPSOLLMENGE3;
            }
            set
            {
                if (value != _ABPSOLLMENGE3)
                {
                     _ABPSOLLMENGE3 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE3");
                }
            }
        }
        private string  _ABPSOLLMENGE4="0";
        [Column]
        public string ABPSOLLMENGE4
        {
            get
            {
                return _ABPSOLLMENGE4;
            }
            set
            {
                if (value != _ABPSOLLMENGE4)
                {
                     _ABPSOLLMENGE4 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE4");
                }
            }
        }
        private string  _ABPSOLLMENGE5="0";
        [Column]
        public string ABPSOLLMENGE5
        {
            get
            {
                return _ABPSOLLMENGE5;
            }
            set
            {
                if (value != _ABPSOLLMENGE5)
                {
                     _ABPSOLLMENGE5 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE5");
                }
            }
        }
        private string  _ABPSOLLMENGE6="0";
        [Column]
        public string ABPSOLLMENGE6
        {
            get
            {
                return _ABPSOLLMENGE6;
            }
            set
            {
                if (value != _ABPSOLLMENGE6)
                {
                     _ABPSOLLMENGE6 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE6");
                }
            }
        }
        private string  _ABPSOLLMENGE7="0";
        [Column]
        public string ABPSOLLMENGE7
        {
            get
            {
                return _ABPSOLLMENGE7;
            }
            set
            {
                if (value != _ABPSOLLMENGE7)
                {
                     _ABPSOLLMENGE7 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE7");
                }
            }
        }
        private string  _ABPSOLLMENGE8="0";
        [Column]
        public string ABPSOLLMENGE8
        {
            get
            {
                return _ABPSOLLMENGE8;
            }
            set
            {
                if (value != _ABPSOLLMENGE8)
                {
                     _ABPSOLLMENGE8 = value;
                    NotifyPropertyChanged("ABPSOLLMENGE8");
                }
            }
        }
        private string  _ABPLIEFDAT="0";
        [Column]
        public string ABPLIEFDAT
        {
            get
            {
                return _ABPLIEFDAT;
            }
            set
            {
                if (value != _ABPLIEFDAT)
                {
                     _ABPLIEFDAT = value;
                    NotifyPropertyChanged("ABPLIEFDAT");
                }
            }
        }
        private string  _ABPSTARTDAT="0";
        [Column]
        public string ABPSTARTDAT
        {
            get
            {
                return _ABPSTARTDAT;
            }
            set
            {
                if (value != _ABPSTARTDAT)
                {
                     _ABPSTARTDAT = value;
                    NotifyPropertyChanged("ABPSTARTDAT");
                }
            }
        }
        private string  _ABPNEUDAT="0";
        [Column]
        public string ABPNEUDAT
        {
            get
            {
                return _ABPNEUDAT;
            }
            set
            {
                if (value != _ABPNEUDAT)
                {
                     _ABPNEUDAT = value;
                    NotifyPropertyChanged("ABPNEUDAT");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<ABPOS> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\ABPOS\\ABPOS" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\ABPOS" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("ABPOS");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (ABPOS row in ListInput)
                {
                    XElement f = new XElement("ABPOS");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement ABPMANDANT = new XElement("ABPMANDANT");
                         ABPMANDANT.Value = row.ABPMANDANT;
                          f.Add(ABPMANDANT);
                        XElement ABPAUFKRS = new XElement("ABPAUFKRS");
                         ABPAUFKRS.Value = row.ABPAUFKRS;
                          f.Add(ABPAUFKRS);
                        XElement ABPAUFLFD = new XElement("ABPAUFLFD");
                         ABPAUFLFD.Value = row.ABPAUFLFD;
                          f.Add(ABPAUFLFD);
                        XElement ABPAUFPOS = new XElement("ABPAUFPOS");
                         ABPAUFPOS.Value = row.ABPAUFPOS;
                          f.Add(ABPAUFPOS);
                        XElement ABPWAKZ = new XElement("ABPWAKZ");
                         ABPWAKZ.Value = row.ABPWAKZ;
                          f.Add(ABPWAKZ);
                        XElement ABPART = new XElement("ABPART");
                         ABPART.Value = row.ABPART;
                          f.Add(ABPART);
                        XElement ABPVAR = new XElement("ABPVAR");
                         ABPVAR.Value = row.ABPVAR;
                          f.Add(ABPVAR);
                        XElement ABPFARBE = new XElement("ABPFARBE");
                         ABPFARBE.Value = row.ABPFARBE;
                          f.Add(ABPFARBE);
                        XElement ABPSOLLMENGE1 = new XElement("ABPSOLLMENGE1");
                         ABPSOLLMENGE1.Value = row.ABPSOLLMENGE1;
                          f.Add(ABPSOLLMENGE1);
                        XElement ABPSOLLMENGE2 = new XElement("ABPSOLLMENGE2");
                         ABPSOLLMENGE2.Value = row.ABPSOLLMENGE2;
                          f.Add(ABPSOLLMENGE2);
                        XElement ABPSOLLMENGE3 = new XElement("ABPSOLLMENGE3");
                         ABPSOLLMENGE3.Value = row.ABPSOLLMENGE3;
                          f.Add(ABPSOLLMENGE3);
                        XElement ABPSOLLMENGE4 = new XElement("ABPSOLLMENGE4");
                         ABPSOLLMENGE4.Value = row.ABPSOLLMENGE4;
                          f.Add(ABPSOLLMENGE4);
                        XElement ABPSOLLMENGE5 = new XElement("ABPSOLLMENGE5");
                         ABPSOLLMENGE5.Value = row.ABPSOLLMENGE5;
                          f.Add(ABPSOLLMENGE5);
                        XElement ABPSOLLMENGE6 = new XElement("ABPSOLLMENGE6");
                         ABPSOLLMENGE6.Value = row.ABPSOLLMENGE6;
                          f.Add(ABPSOLLMENGE6);
                        XElement ABPSOLLMENGE7 = new XElement("ABPSOLLMENGE7");
                         ABPSOLLMENGE7.Value = row.ABPSOLLMENGE7;
                          f.Add(ABPSOLLMENGE7);
                        XElement ABPSOLLMENGE8 = new XElement("ABPSOLLMENGE8");
                         ABPSOLLMENGE8.Value = row.ABPSOLLMENGE8;
                          f.Add(ABPSOLLMENGE8);
                        XElement ABPLIEFDAT = new XElement("ABPLIEFDAT");
                         ABPLIEFDAT.Value = row.ABPLIEFDAT;
                          f.Add(ABPLIEFDAT);
                        XElement ABPSTARTDAT = new XElement("ABPSTARTDAT");
                         ABPSTARTDAT.Value = row.ABPSTARTDAT;
                          f.Add(ABPSTARTDAT);
                        XElement ABPNEUDAT = new XElement("ABPNEUDAT");
                         ABPNEUDAT.Value = row.ABPNEUDAT;
                          f.Add(ABPNEUDAT);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
